package com.doctor.windflower_doctor.recordfragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.adapter.BloodPressureAdapter;
import com.doctor.windflower_doctor.adapter.StatisticalListAdapter;
import com.doctor.windflower_doctor.entity.BloodPressureBeen;
import com.doctor.windflower_doctor.entity.Notes;
import com.doctor.windflower_doctor.fragment.BaseFragment;
import com.doctor.windflower_doctor.myuser.tools.HttpclientHelper;
import com.doctor.windflower_doctor.util.Constent;

public class BloodPressureFragment extends BaseFragment implements Constent {
	private static BloodPressureFragment baseFragment;
	private ListView listView;
	private TextView unit_text;
	private BloodPressureAdapter bloodPressureAdapter;

	public static BloodPressureFragment getInstance(Bundle bundle) {
		baseFragment = new BloodPressureFragment();
		baseFragment.setArguments(bundle);
		return baseFragment;
	}

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_weight_record, container,
				false);
	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {

	}

	@Override
	protected void findViews(View view) {
		bloodPressureAdapter = new BloodPressureAdapter(getActivity());
		listView = (ListView) view.findViewById(R.id.listView);
		listView.setDividerHeight(0);
		listView.setAdapter(bloodPressureAdapter);
		unit_text = (TextView) view.findViewById(R.id.unit_text);
		unit_text.setText("血压");
		new BloodAsync()
				.execute(RECORD_URL_ONLINE+BLOODPRESSURE+SystemApplication.getInstance().doctorId);
	}

	class BloodAsync extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.getAddress(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				bloodPressureAdapter.addList(bloodJson(result));
			} else {
				Toast.makeText(getActivity(), "请检查网络状态", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}

	public List<BloodPressureBeen> bloodJson(String json) {
		List<BloodPressureBeen> list = new ArrayList<BloodPressureBeen>();

		try {
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.getBoolean("success")) {
				JSONObject jsonObject2 = jsonObject.getJSONObject("data");
				JSONArray array = jsonObject2.getJSONArray("lists");
				for (int i = 0; i < array.length(); i++) {
					BloodPressureBeen been = new BloodPressureBeen();
					JSONObject jsonObject3 = array.getJSONObject(i);
					been.setBloodPressure(jsonObject3
							.getString("bloodPressure"));
					been.setCreatedAt(jsonObject3.getString("createdAt"));
					list.add(been);
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
